package lc.com.architecture.dagger.module;

import android.support.design.internal.ParcelableSparseArray;
import android.util.ArrayMap;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import dagger.Module;
import retrofit2.Retrofit;

/**
 * @author LiuChao
 * @describe
 * @date 2017/11/3
 * @contact email:450127106@qq.com
 */
@Module
public class ServiceModel {
    private HashMap<String, Object> mServices = new HashMap<>(5);
    private Retrofit mRetrofit;

    @Inject
    public ServiceModel(Retrofit retrofit) {
        mRetrofit = retrofit;
    }

    public <T> T getService(Class<T> serviceClass) {
        T service = null;
        synchronized (mServices) {
            service = (T) mServices.get(serviceClass.getName());
            if (service == null) {
                service = mRetrofit.create(serviceClass);
                mServices.put(serviceClass.getName(), service);
            }
        }
        return service;
    }


}
